package com.learner.course.service.impl;

import com.learner.course.repository.CourseBaseRepository;
import com.learner.course.repository.TeacherRepository;
import com.learner.course.service.CourseBaseService;
import com.learner.pojo.course.CourseBase;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author ice2020x
 * @Date: 2023/3/9
 * @Description: 基础课程信息
 **/
@Service
@Slf4j
public class CourseBaseServiceImpl implements CourseBaseService {

    @Autowired
    private CourseBaseRepository repository;

    @Autowired
    private TeacherRepository teacherRepository;

    @Override
    public CourseBase getCourseBaseById(String courseId) {
        log.info("根据id查询课程基础信息:{}",courseId);
        Optional<CourseBase> optional = repository.findById(courseId);
        return optional.orElse(null);
    }


    /**
     * @Description: 根据id修改课程信息
     * @Author: ice2020x
     * @Date: 2021/11/12
     */
    @Override
    @Transactional
    public CourseBase updateCourseBase(String id, CourseBase courseInfo) {
        log.info("根据id修改课程信息,id:{}",id);
        courseInfo.setId(id);
        return repository.save(courseInfo);
    }


    @Override
    public CourseBase saveCourseBase(CourseBase courseInfo) {
        return repository.save(courseInfo);
    }

    @Override
    public List<CourseBase> getCourseListByTeacherId(String teacherId, int pageNum, int pageSize) {
        List<CourseBase> courseBase = repository.findByUserId(teacherId);
        System.out.println(pageSize);
        return courseBase.stream().skip((long) (pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
    }

}
